package database

import (
	"database/sql"
	"fmt"
	"github.com/jmoiron/sqlx"

	// 导入驱动
	_ "github.com/go-sql-driver/mysql"
)

func InitDB() (err error, db *sql.DB) {

	dns := "root:where1234@tcp(127.0.0.1:3306)/guli"

	// 并没有真正发起连接
	db, err = sql.Open("mysql", dns)

	if err != nil {
		panic(err)
	}

	err = db.Ping()

	if err != nil {
		return err, nil
	}
	//db.SetMaxIdleConns() // 最大空闲连接数
	//db.SetConnMaxLifetime()
	fmt.Println("connection successful")
	return nil, db
}

func InitDB2() (err error, db *sqlx.DB) {
	dns := "root:where1234@tcp(127.0.0.1:3306)/guli"
	db, err = sqlx.Connect("mysql", dns)
	//sqlx.MustConnect()
	return err, db
}
